package com.sdgs.app.service;

import java.util.Date;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sdgs.app.base.BaseDao;
import com.sdgs.app.base.BasePage;
import com.sdgs.app.bean.UserBean;
import com.sdgs.app.page.Page;
import com.sdgs.app.repository.UserMapper;
import com.sdgs.paas.sdk.core.PaasBaseException;
import com.sdgs.paas.sdk.core.mybatis.EnablePaasMybatis;

@EnablePaasMybatis
@MapperScan("com.sdgs.app.repository")
@Service
public class UserService extends BaseDao<UserBean, UserMapper, Integer>{
	@Override
	public String getMapperNamespace() {
		return UserMapper.class.getName();
	}

	@Autowired
	private UserMapper userMapper;
	
	public Page<UserBean> getPageFileList(BasePage pageRequest, UserBean userBean) {
		userBean.setIsDelete(0);
		pageRequest.setFilters(userBean);
		Page<UserBean> page = pageQuery(pageRequest);
		return page;
	}

	@Transactional(rollbackFor = Exception.class)
	public boolean insert(UserBean file) {
		try {
            file.setCreateTime(new Date());
            file.setUpdateTime(new Date());
			int count = userMapper.insertUser(file);
			return count == 1;
		} catch (Exception e) {
			throw new PaasBaseException(e, "500", "数据库插入异常！");
		}
	}

	@Transactional(rollbackFor = Exception.class)
	public boolean updateUser(UserBean user) {
		try {
			user.setUpdateTime(new Date());
			int count = userMapper.updateUser(user);
			return count == 1;
		} catch (Exception e) {
			throw new PaasBaseException(e, "500", "数据库插入异常！");
		}
	}

	public UserBean getUser(String id) {
		return userMapper.getUserDetail(id);
	}
}
